package com.jgdt.demo.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import com.librariy.util.SystemDisplay;
import com.librariy.view.IconTextView;
/**
 * 类似CheckedTextView，可带（前/后/左/右）图标，且紧跟文字显示
 *<com.jgdt.demo.view.CellTipsView
 *           android:id="@+id/mHouseType"
 *           android:layout_width="0dp"
 *           android:layout_height="match_parent"
 *           android:layout_weight="1"
 *           android:drawablePadding="5dp"
 *           android:drawableRight="@drawable/ic_sort_selector"
 *           android:singleLine="true"
 *           android:checked="true"
 *           android:text="新房"
 *           android:textColor="@color/color_text_checked_default"
 *           android:textSize="@dimen/textSizeMiddle" /> 
 *
 * @author luchyu
 * */

public class CellTipsView extends IconTextView{
    private RectF mRect=new RectF(0,0,0,0);
    private SystemDisplay sd=SystemDisplay.getInstance(null);
    private Paint mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
    private CharSequence mTips="";
    private int mStyle=1;
    public CellTipsView(Context context) {
        super(context);
    }
    public CellTipsView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CellTipsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void setTips(int mTips) {
        this.mTips=mTips>0?String.valueOf(mTips):"";
        this.postInvalidate();
    }
    public void setTips(CharSequence mTips) {
        this.mTips=mTips;
        this.postInvalidate();
    }
    public void setStyle(int mStyle) {
        this.mStyle=mStyle;
        this.postInvalidate();
    }
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if(TextUtils.isEmpty(mTips)){
            return;
        }
        if(mStyle==0){
            dispatchDraw0(canvas);
        }else{
            dispatchDraw1(canvas);
        }
    }
    protected void dispatchDraw0(Canvas canvas) {
        int radius=(int)(6*sd.density);
        int x=mCheckedTextView.getRight()-(int)(7*sd.density);
        int y=mCheckedTextView.getTop()+0;
        mRect.set(x-radius,y+radius,x+radius,y-radius);
        //画圆
        mPaint.setColor(0xFFFF0000);
        canvas.drawCircle(mRect.centerX(),mRect.centerY(),radius,mPaint);
    }
    protected void dispatchDraw1(Canvas canvas) {
        int radius=(int)(10*sd.density);
        int x=mCheckedTextView.getRight()-radius;
        int y=mCheckedTextView.getTop()+0;
        mRect.set(x-radius,y+radius,x+radius,y-radius);
        //画圆
        mPaint.setColor(0xFFFF0000);
        canvas.drawCircle(mRect.centerX(),mRect.centerY(),radius,mPaint);
        //画字符
        mPaint.setColor(0xFFFFFFFF);
        mPaint.setTextSize(10 * sd.density);
        mPaint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fm = mPaint.getFontMetrics();
        float offset = (fm.bottom - fm.top) / 2 - fm.bottom;
        canvas.drawText(mTips+"", mRect.centerX(), mRect.centerY() + offset, mPaint);
    }
}
